package com.crm.controller;

import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.common.util.Page;
import com.crm.entity.BaseDict;
import com.crm.entity.Customer;
import com.crm.entity.QueryVo;
import com.crm.service.BaseDictService;
import com.crm.service.CustomerService;


@Controller
public class CustomerController {
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private CustomerService customerService;
	@Value("${fromType.code}")
	private String fromTypeCode;
	@Value("${industryType.code}")
	private String industryTypeCode;
	@Value("${levelType.code}")
	private String levelTypeCode;
	
	
	
	@RequestMapping(value= {"/customer/list","/"})
	public String list(QueryVo queryVo,Model model) {
		List<BaseDict> fromType = baseDictService.selectBaseDictListByCode(fromTypeCode);
		List<BaseDict> industryType = baseDictService.selectBaseDictListByCode(industryTypeCode);
		List<BaseDict> levelType = baseDictService.selectBaseDictListByCode(levelTypeCode);
		model.addAttribute("fromType", fromType);
		model.addAttribute("industryType", industryType);
		model.addAttribute("levelType", levelType);
		
		Page<Customer> page =customerService.selectPageByQueryVo(queryVo);
		model.addAttribute("custName", queryVo.getCustName());
		model.addAttribute("custSource", queryVo.getCustSource());
		model.addAttribute("custIndustry", queryVo.getCustIndustry());
		model.addAttribute("custLevel", queryVo.getCustLevel());
		
		
		model.addAttribute("page",page);
		
		return "customer";
	}
	
	@RequestMapping("customer/delete")
	@ResponseBody
	public String del(Long id) {
		customerService.delCustomerById(id);
		return "OK";
	}
	@RequestMapping("customer/update")
	@ResponseBody
	public String update(Customer customer) {
		customerService.updateCustomerById(customer);
		return "OK";
	}
	@RequestMapping("customer/edit")
	@ResponseBody
	public Customer edit(Long id) {
		return customerService.selectCustomerById(id);
	}
}
